new Vue({
    el: '#app',
    data(){
        return {
            multipleSelection:[],// 多选id值
            patients:[], // 患者列表
            activeIndex: '1' ,//活动页
            searchByCondition: { // 条件查询
                name: '',
                id: '',
                gender:'',
              },
              updateFormVisible: false,//修改显示
              updateForm: {  // 修改表单
                name: '',
                id:'',
                gender : '',
              },

              addFormVisible: false,//添加显示
                addForm: {  // 添加表单
                name: '',
                id:'',
                gender : '',
              },
              formLabelWidth: '120px',
              // 规则
              rules:{
                name: [
                    { required: true, message: '请输入患者名称', trigger: 'blur' },
                  ],
                  id: [
                    { required: true, message: '请输入患者身份证号', trigger: 'blur' },
                  ],
                  gender: [
                    { required: true, message: '请选择性别', trigger: 'blur' },
                  ],
              }
        }
    },
    methods: {
        // 导航
        handleSelect(key, keyPath) {
            if(key==2){
                 location.href="doctor.html"
            }else if(key==3){
                location.href="bill.html"
            }else{
                location.href="registration.html"
            }
           
          },
        // 编辑
        handleEdit(index, row) {
            this.updateFormVisible=true
            // 数据回写
            this.updateForm=row
        },
        // 删除
        handleDelete(index, row) {
          console.log(index, row);
          let _this = this;
          this.$confirm('此操作将永久删除该患者信息, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            axios(
                {
                    method:"GET",
                    url: "http://localhost/patients/delete/"+row.id,
                }
            ).then(function(resp){
                console.log(resp.data);
               _this.getAllPatients();
               _this.$message({
                message: resp.data.message,
                type:"success"
            })
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });          
          });



 
        },
        // 修改患者信息
        update() {
            let _this = this;
            axios(
                {
                    method:"POST",
                    url: "http://localhost/patients/update",
                    data:_this.updateForm
                }
            ).then(function(resp){
                console.log(resp.data);
               _this.getAllPatients();
               _this.updateFormVisible=false;
               _this.$message({
                message: resp.data.message,
                type:"success"
            })
            })
          },

         // 添加患者信息
        add() {
            let _this = this;
            if(this.addForm.name==""|this.addForm.id==""|this.addForm.gender==""){
                _this.$message({
                    message: "请填写完整信息",
                    type:"error"
                })
                return;
            }
            axios(
                {
                    method:"POST",
                    url: "http://localhost/patients/add",
                    data:_this.addForm
                }
            ).then(function(resp){
                console.log(resp.data);
               _this.getAllPatients();
               _this.addFormVisible=false;
               if(resp.data.success){
                _this.$message({
                    message: resp.data.message,
                    type:"success"
                })
               }else{
                _this.$message({
                    message: resp.data.message,
                    type:"error"
                })
               }

            }).catch(function(resp){
                _this.$message({
                    message: "身份证格式错误",
                    type:"error"
                })
            })
          },
          
          // 批量删除
        deletes(){
            let _this = this;
            let ids=[]
            _this.multipleSelection.forEach(item=>{
                ids.push(item.id)
            })
            this.$confirm('此操作将永久删除该患者信息, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              axios(
                  {
                      method:"POST",
                      url: "http://localhost/patients/delete/ids",
                      data: ids
                  }
              ).then(function(resp){
                _this.multipleSelection=[];
                  console.log(resp.data);
                 _this.getAllPatients();
                 _this.$message({
                  message: resp.data.message,
                  type:"success"
              })
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              });          
            });
  
        },
        handleSelectionChange(val) {
            this.multipleSelection=val
          },
          // 条件查询
          getByCondition(){
            let _this = this;
            axios(
                {
                    method:"POST",
                    url: "http://localhost/patients/get/condition",
                    data:_this.searchByCondition
                }
            ).then(function(resp){
                console.log(resp.data);
                _this.patients=resp.data.data;
            })
        }
          ,
            // 根据医生id查询患者
          getPatientsByDoctorId(id){
            let _this = this;
            axios(
                {
                    method:"GET",
                    url: "http://localhost/patients/all/patients/"+id,
                }
            ).then(function(resp){
                console.log(resp.data);
                _this.patients=resp.data.data;
            })
          },
        // 获得所有病人
        getAllPatients(){
            let _this = this;
            axios(
                {
                    methods:"get",
                    url: "http://localhost/patients/all/",
                }
            ).then(function(resp){
                console.log(resp.data);
                _this.patients=resp.data.data;
                _this.searchByCondition={};// 清空搜索框
            })
        }
      },

      mounted(){

        var param = location.search;
        if(param==""){
            this.getAllPatients();
        }
        console.log(param)
        type=param.slice(1,param.indexOf("="))
        id=param.slice(param.indexOf("=")+1)
        if(type=="doctorId"){
            this.getPatientsByDoctorId(id)
        }
    }
})